package jp.ac.aiit.spg.entity;

import java.io.Serializable;
import java.util.*;
import javax.persistence.*;

@Entity
public class SpgToDo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// version ***********************************
	@Version
	private Integer version;

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	
	// id ***********************************
	@Id
	@GeneratedValue
	private Integer id;

	public Integer getId() {
		return this.id;
	}
	
	// ユーザー（１人）***********************
	@OneToOne
	private SpgUser user;

	public SpgUser getUser() {
		return user;
	}

	public void setUser(SpgUser user) {
		this.user = user;
	}

	// タスク（複数） ***********************
	
	// eager fetch 指定解除 
	@OneToMany(mappedBy="todo", fetch = FetchType.EAGER)
	private List<SpgTask> tasks = new ArrayList<SpgTask>();

	
	public List<SpgTask> getTasks(){
		return this.tasks;
	}

	public void setTasks(List<SpgTask> tasks) {
		this.tasks = tasks;
	}
	
	public void addTask(SpgTask st) {
		this.tasks.add(st);
	}
		
	public void removeTask(SpgTask st) {
		this.tasks.remove(st);
	}
	
	public void sortTasks(String[] sortOrder){
		for(int i =0;i<sortOrder.length-1;i++){
			if(sortOrder[i]=="spgAssignStatus"){
				Collections.sort(tasks, new Comparator<SpgTask>() {
		            public int compare(SpgTask task1, SpgTask task2) {
		            	if(task1.getAssignment()==null&& task2.getAssignment()==null){
		            		return 0;
		            	}else if(task1.getAssignment()==null){
		            		return -1;
		            	}else if(task2.getAssignment()==null){
		            		return 1;
		            	}
		                return task1.getAssignment().compareTo(task2.getAssignment());
		            }
				});
			}else if(sortOrder[i]=="startTimePlanned"){
				Collections.sort(tasks, new Comparator<SpgTask>() {
					public int compare(SpgTask task1, SpgTask task2) {
		            	if(task1.getStartTimePlanned()==null&& task2.getStartTimePlanned()==null){
		            		return 0;
		            	}else if(task1.getStartTimePlanned()==null){
		            		return -1;
		            	}else if(task2.getStartTimePlanned()==null){
		            		return 1;
		            	}
						return task1.getStartTimePlanned().compareTo(task2.getStartTimePlanned());
					}
				});
			}else if(sortOrder[i]=="projectId"){
				Collections.sort(tasks, new Comparator<SpgTask>() {
					public int compare(SpgTask task1, SpgTask task2) {
		            	if(task1.getWbs().getProject().getId()==null&& task2.getWbs().getProject().getId()==null){
		            		return 0;
		            	}else if(task1.getWbs().getProject().getId()==null){
		            		return -1;
		            	}else if(task2.getWbs().getProject().getId()==null){
		            		return 1;
		            	}
						return task1.getWbs().getProject().getId().compareTo(task2.getWbs().getProject().getId());
					}
				});
			}
		}
	}
	
	
}
